<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <div id="app">
    <cpn></cpn>
  </div>

  <template id="cpn">
    <div>
      <ccpn></ccpn>
    </div>
  </template>

  <template id="ccpn">
    <div>
      <h3>我是子组件</h3>
      <button @click="btnClick">访问父组件</button>
    </div>
  </template>

  <script src="../../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {},
      // 子组件注册 --> Vue
      components: {
        cpn: {
          template: '#cpn',
          data() {
            return {
              name: "尘世峰"
            }
          },
          // 子组件注册 --> cpn
          components: {
            ccpn: {
              template: '#ccpn',
              methods: {
                btnClick() {
                  // 1. this.$parent 访问父组件
                  console.log(this.$parent);
                  console.log("父组件name属性值", this.$parent.name);

                  // 2. this.$root 访问根组件 第一层组件
                  console.log(this.$root);
                }
              },
            }
          }
        }
      }
    })
  </script>
</body>

</html>